package com.xxx.seckill.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: xxx
 * @Date: 2023/1/9 20:06
 * @Description: 向rabbitmq发送消息
 */
@Service
@Slf4j
public class MQSender {
    
    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 通过header模式发送消息
     */
    public void send06(String msg){
        log.info("发送消息headers(被queue01接收):"+msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color", "red");
        properties.setHeader("speed", "normal");
        Message message = new Message(msg.getBytes(), properties);
        rabbitTemplate.convertAndSend("headersExchange", "", message);
    }

    /**
     * 通过header模式发送消息
     */
    public void send05(String msg){
        log.info("发送消息headers(被两个queue接收):"+msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color", "red");
        properties.setHeader("speed", "fast");
        Message message = new Message(msg.getBytes(), properties);
        rabbitTemplate.convertAndSend("headersExchange", "", message);
    }

    /**
     * 通过topic模式发送消息
     */
    public void send04(Object msg){
        log.info("发送消息(被两个queue接收):"+msg);
        rabbitTemplate.convertAndSend("topicExchange", "message.queue.green.abc", msg);
    }


    /**
     * 通过topic模式发送消息
     */
    public void send03(Object msg){
        log.info("发送消息(QUEUE01接收):"+msg);
        rabbitTemplate.convertAndSend("topicExchange", "queue.red.message", msg);
    }


    /**
     * 通过路由键发送消息
     */
    public void send02(Object msg){
        log.info("发送green的消息："+msg);
        rabbitTemplate.convertAndSend("directExchange","queue.green",msg);
    }
    

    /**
     * 通过路由键发送消息
     */
    public void send01(Object msg){
        log.info("发送red的消息："+msg);
        rabbitTemplate.convertAndSend("directExchange","queue.red",msg);
    }
    

    /**
     * 发送到交换机
     * @param msg
     */
    public void send1(Object msg){
        log.info("发送消息:"+msg);
        rabbitTemplate.convertAndSend("fanoutExchange","",msg);
    }
    
    
    public void send(Object msg){
        log.info("发送消息:"+msg);
        rabbitTemplate.convertAndSend("queue",msg);
    }


    
    
    
}
